# ====================以下部分代码只作开发测试用====================================
import django
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'meiduo.settings.dev_settings')
django.setup()  # 加载django环境
# =============================================================================

from django.conf import settings
from django.template import loader

from contents.utils import get_ad_content
from goods.utils import get_goods_channel_content


def generate_static_index_html():
    """
    生成静态主页html
    :return:
    """
    categories = get_goods_channel_content()  # 获取主页商品频道数据
    contents = get_ad_content()  # 获取主页广告数据
    context = {'categories': categories, 'contents': contents}  # 构建主页模板渲染内容
    template = loader.get_template('index.html')  # 加载主页模板'index.html'
    html_text = template.render(context)  # 将主页内容"context"渲染到模板中，生成主城主页html
    file_path = os.path.join(
        settings.GENERATED_STATIC_HTML_FILES_DIR,
        'index.html'
    )  # 拼接主页html文件的存储路径
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_text)  # 将主页html文件写入到文件中


if __name__ == '__main__':
    generate_static_index_html()
